import { InputQuestion, QuestionBase, SelectQuestion, TextQuestion, TimeQuestion } from '@/components/public-template';
import { regExp } from '@/utils/regExp';
import { Api as ApiSystem } from '@/config/api/system';
import dayjs from 'dayjs';

class HomeService {
  getQuestions() {
    const questions: QuestionBase<any>[] = [
      new InputQuestion({
        key: 'name',
        label: '计划名称',
        required: true,
        placeholder: '请输入',
        reg: regExp.len020,
        prompt: '计划名称不可为空，至多允许输入20个字符',
        value: null
      }),
      new InputQuestion({
        key: 'description',
        label: '描述',
        value: null,
        reg: regExp.len100,
        prompt: '描述至多允许输入100字符'
      }),
      new SelectQuestion({
        key: 'dept',
        label: '部门',
        required: true,
        value: [],
        type: 'tree',
        prompt: '请选择',
        state: {
          fullPath: true,
          url: ApiSystem.orgTreeData,
          protType: 'post',
          nameKey: 'name',
          nameValue: 'id',
          nameChildren: 'childOrg',
          treeCheckable: true
        }
      }),
      new SelectQuestion({
        key: 'men',
        label: '执行人',
        required: true,
        prompt: '请选择',
        value: [],
        state: {
          url: ApiSystem.subjectListSimple,
          protType: 'post',
          nameKey: 'nickName',
          nameValue: 'id',
          mode: 'multiple',
          showSearch: true,
          optionFilterProp: 'label'
        }
      }),
      new InputQuestion({
        key: 'frequencyNum',
        label: '执行频率',
        required: true,
        reg: regExp.maxdigital3,
        prompt: '执行频率不可为空，至多允许输入3位数正整数',
        type: 'number',
        value: null,
        state: {
          extendParams: {
            frequencyUnit: 1
          },
          addonAfter: {
            key: 'frequencyUnit',
            tagType: 'select',
            type: 'dictionary',
            filterName: 'CYCLE',
            valueType: 'number',
            options: []
          }
        }
      }),
      new InputQuestion({
        key: 'warningPeriod',
        label: '预警期（天）',
        required: true,
        value: null,
        type: 'number',
        reg: (rule, value, formState, formRef) => {
          if (value === null) {
            return Promise.reject('请输入');
          } else if (!regExp.maxdigital3.test(value)) {
            return Promise.reject('至多允许输入3位数正整数');
          } else {
            return Promise.resolve();
          }
        }
      }),
      new TimeQuestion({
        key: 'planCreateTime',
        label: '计划开始时间',
        value: dayjs().format('YYYY-MM-DD'),
        type: 'alone',
        required: true,
        prompt: '请输入',
        state: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD'
        }
      }),
      new TimeQuestion({
        key: 'planEndTime',
        label: '计划结束时间',
        value: null,
        type: 'alone',
        required: true,
        // prompt: '请输入',
        reg: (rule, value, formState, formRef) => {
          if (value === null) {
            return Promise.reject('请选择');
          } else {
            if (new Date(formState.planCreateTime).getTime() > new Date(formState.planEndTime).getTime()) {
              return Promise.reject('计划结束时间不能大于计划开始时间');
            }
            return Promise.resolve();
          }
        },
        state: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD'
        }
      })
    ];
    return questions;
  }
  getViewQuestions() {
    const questions: QuestionBase<any>[] = [
      new TextQuestion({
        key: 'name',
        label: '计划名称',
        value: null
      }),
      new TextQuestion({
        key: 'dept',
        label: '部门',
        value: []
      }),
      new TextQuestion({
        key: 'men',
        label: '执行人',
        value: []
      }),
      new TextQuestion({
        key: 'frequencyNum',
        label: '执行频率',
        value: null
      }),
      new TextQuestion({
        key: 'warningPeriod',
        label: '预警期（天）',
        value: null
      }),
      new TextQuestion({
        key: 'planCreateTime',
        label: '计划开始时间',
        value: null
      }),
      new TextQuestion({
        key: 'planEndTime',
        label: '计划结束时间',
        value: null
      }),
      new TextQuestion({
        key: 'description',
        label: '描述',
        value: null
      })
    ];
    return questions;
  }
}

export default new HomeService();
